using System;
using System.Diagnostics;
using System.Data;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// Summary description for GameWorld
/// </summary>
public static class GameWorld
{
    public static System.Random rnd;
    private static int lastEntityID = 0;
    private static int worldTicks;
    public static int WorldTicks
    {
        get { return worldTicks; }
        set { worldTicks = value; }
    }

    private static List<GameEntity> GameEntities;

    private static string[][] gameMap;
    private static int MAP_WIDTH, MAP_HEIGHT;


	static GameWorld()
	{
        rnd = new Random((int)DateTime.Now.Ticks);
        WorldTicks = 0;
        // Initialize the list of entities
        GameEntities = new List<GameEntity>();

        // Initialize the game map
        gameMapInit();
        CreateTestEntities();
	}

    private static void gameMapInit(){
        // Initialize the map
        string[] files = System.IO.Directory.GetFiles(System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath + "/maps/", "*.txt");
        List<string[]> maps = new List<string[]>();

        foreach (string myMap in files)
        {
            maps.Add(File.ReadAllLines(myMap));
        }
        gameMap = new string[maps.Count][];
        for (int i = 0; i < maps.Count; i++)
        {
            gameMap[i] = maps[i];
        }
    }

    /// <summary>
    /// This function returns a new player object for somebody who connects
    /// and adds it into the gameworld
    /// </summary>
    /// <returns></returns>
    public static EntityPlayer AddPlayer(string SessionID)
    {
        EntityPlayer newPlayer = new EntityPlayer(lastEntityID++, SessionID, 4, 4, "<SPAN class='playerChar'>O</SPAN>", SessionID, 0);
        GameWorld.RemovePlayer(SessionID); // Make sure we don't have dupes
        GameEntities.Add(newPlayer);
        return newPlayer;
    }

    public static bool RemovePlayer(string SessionID)
    {
        bool success = false;
        for (int i = 0; i < GameEntities.Count; i++)
        {
            if (GameEntities[i].Name == SessionID)
            {
                GameEntities.RemoveAt(i);
                success = true;
                break;
            }
        }
        return success;
    }

    public static void Tick(object source, System.Timers.ElapsedEventArgs e)
    {
        worldTicks++;
        // Iterate through the entities and see if any of them wanna move around
        foreach (GameEntity myEnt in GameEntities)
        {
            if (myEnt.GetType() == typeof(EntityWanderer))
            {
                ((EntityWanderer)myEnt).wander();
            }
        }

    }

    public static List<GameEntity> getEntities()
    {
        return GameEntities;
    }

    public static List<string> getMap(int mapNum)
    {
        List<string> myMap = new List<string>(gameMap[mapNum].Length);
        for (int i = 0; i < gameMap[mapNum].Length; i++)
        {
            myMap.Add(gameMap[mapNum][i]);
        }
        return myMap;
    }

    public static void CreateTestEntities(){
        GameEntities.Add(new EntityWanderer(lastEntityID++, "Karl", ((int)(rnd.NextDouble() * 7) + 1), ((int)(rnd.NextDouble() * 7) + 1), "<SPAN style='color:green'>M</SPAN>", 1));
        GameEntities.Add(new EntityWanderer(lastEntityID++, "Lenin", ((int)(rnd.NextDouble() * 9) + 1), ((int)(rnd.NextDouble() * 9) + 1), "<SPAN style='color:blue'>P</SPAN>", 1));
        GameEntities.Add(new EntityWanderer(lastEntityID++, "Bug", ((int)(rnd.NextDouble() * 9) + 1), ((int)(rnd.NextDouble() * 9) + 1), "<SPAN style='color:red'>F</SPAN>",1));
    }
}
